package com.avicsafety.ezsystem.local_stoage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.avicsafety.ezsystem.R;
import com.avicsafety.ezsystem.entity.User;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 本地存储演示
 * 该页面采用黄油刀..
 */
public class LocalStorageActivity extends AppCompatActivity {

    @BindView(R.id.btn_push) Button btn_push;
    @BindView(R.id.btn_find) Button btn_find;
    @BindView(R.id.btn_delete) Button btn_delete;
    @BindView(R.id.tv_list) TextView tv_list;

    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_stoage);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_push) void onPush(){
        count++;

        User user = new User();
        user.setGuid(new Date().getTime()+"");
        user.setFirstName("user"+count);
        user.setLastName("id"+count);

        LocalStorage.pushModel("user","guid",user,new String[]{"lastName"},new String[]{user.getLastName()});
        tv_list.setText("");

        for(User _user:LocalStorage.findAll("user",User.class,null,null,null,null)){
            tv_list.setText(tv_list.getText()+","+_user.toString());
        }

    }

    @OnClick(R.id.btn_delete) void onDelete(){
        LocalStorage.deleteTable();
        tv_list.setText("");
    }

    @OnClick(R.id.btn_find) void onFind(){
        tv_list.setText("");
        String[][] whe = {{"lastName","id3"}};
        for(User user:LocalStorage.findAll("user",User.class,null,"sort1 desc",3,0)){
            tv_list.setText(tv_list.getText()+","+user.toString());

        }
    }

}
